package com.ars.listStatistics;

import com.ars.db.DBTools;
import com.sun.crypto.provider.RSACipher;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import jdk.nashorn.internal.ir.WhileNode;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class ShowWebsiteListController implements Initializable {
    DBTools db = null;
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn oidColumn;
    @FXML
    private TableColumn passenger_nameColumn;
    @FXML
    private TableColumn passenger_idColumn;
    @FXML
    private TableColumn seats_typeColumn;
    @FXML
    private TableColumn ticket_typeColumn;
    @FXML
    private TableColumn dateColumn;
    @FXML
    private TableColumn fidColumn;
    @FXML
    private ComboBox cbWebsiteName;
    private String WebsiteName = null;
    private int Wid;

    private int Wid2;

    String websitename[]= new String[20];
    private int getWid() {
        return Wid;
    }

    private void setWid(int wid) {
        Wid = wid;
    }


    @FXML
    public void showButtonAction() {
    	 WebsiteName = (String) cbWebsiteName.getValue();
    	 db = new DBTools();
        String sql2="select * from website";
        ResultSet rs=db.query(sql2);

        try {
			while(rs.next())
			{
				if(WebsiteName.equals(rs.getString("name"))){
					Wid2=rs.getInt("wid");
					setWid(Wid2);
				}
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
        //查询某个网点的订单
       this.init();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
//    	cbWebsiteName.getItems().clear();
    	 db = new DBTools();
         String sql="select * from website where `limit`=3";
         ResultSet rs=db.query(sql);
         try {
			while(rs.next()){
				 cbWebsiteName.getItems().add(rs.getString("name"));
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //初始化查询所有网点订单
        this.init();
    }

    private ObservableList getData(int i) {
        ObservableList data = FXCollections.observableArrayList();
        //查询某个网点订单
        String sql = "select * from orders where wid='" + i + "'";
        if (i == 0) {//查询所有网点订单
            sql = "select * from orders";
        }

        db = new DBTools();
        ResultSet rs = db.query(sql);
        try {
            while (rs.next()) {
                Order order = new Order();
                order.setOid(rs.getString("oid"));
                order.setPassenger_name(rs.getString("passenger_name"));
                order.setPassenger_id(rs.getString("passenger_id"));
                order.setSeats_type(rs.getInt("seats_type"));
                order.setTicket_type(rs.getInt("ticket_type"));
                order.setDate(rs.getDate("date"));
                order.setFid(rs.getString("fid"));

                data.add(order);

            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "获取信息失败！", "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            db.close();
        }
        return data;
    }

    //对网点订单的初始化
    public void init(){
    	oidColumn.setCellValueFactory(new PropertyValueFactory<>("Oid"));
        passenger_nameColumn.setCellValueFactory(new PropertyValueFactory<>("Passenger_name"));
        passenger_idColumn.setCellValueFactory(new PropertyValueFactory<>("Passenger_id"));
        seats_typeColumn.setCellValueFactory(new PropertyValueFactory<>("Seats_type"));
        ticket_typeColumn.setCellValueFactory(new PropertyValueFactory<>("Ticket_type"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("Date"));
        fidColumn.setCellValueFactory(new PropertyValueFactory<>("Fid"));

        tableView.setItems(getData(getWid()));
    }
}

